<?php

class SupplierModel extends Model

{

    //创建供货商，生成后加入到站点，关联会员，关联角色

	public function create1($supplier_id, $role_id, $member, $site_id, $status = 0){

		//提取会员信息作为用户信息

		

		$user['member_id'] = $member['id'];

		$finduser = D('User') -> where('member_id = '.$member['id']) -> find();

		if($finduser){

			//$this->error = '用户已经存在';

			//return false;

			$userid = $finduser['id'];

		}else{

			$user['password'] = $member['password'];

			$user['member_id'] = $member['id'];

            $user['nickname'] = $member['nickname'];

            $user['account'] = $member['nickname'];

			//$user['email'] = $member['Email'];

			$user['status'] = 0;

			$userid = D('User') -> add($user);

		}

		

		//关联角色，这个角色必须存在

		$role = M('Role') -> where(array('id' => $role_id)) -> find();

		if($role){

			$roleuser['user_id'] = $userid;

			$roleuser['role_id'] = $role_id;

			M('Role_user') -> add($roleuser);

		}

		//让这个用户可以管理到当前的站点

		$site = M('Site') -> where(array('id' => $site_id)) -> find();

		if($site){

			$siteuser['site_id'] = $site_id;

			$siteuser['user_id'] = $userid;

			if(!D('Site_user')->where('site_id = '.$site_id.' and user_id = '.$userid)->find()){

				M('Site_user') -> add($siteuser);

			}

		}

		//关联一个供货商信息，绑定供货商和后台管理用户

		$supplier = M('Supplier') -> where(array('id' => $supplier_id)) -> find();

		$supplier_user['user_id'] = $userid;

		$supplier_user['supplier_id'] = $supplier['id'];

		M('Supplier_user') -> add($supplier_user);

	}

	

	//创建收款二维码

    public function createqr($supplier_id = 0){

		$supplier = $this->where(array('id' => $supplier_id)) -> find();

		if($supplier['type'] == 1){

			return false;

		}

		

		$array = array ('title' => '我的收款码', 'remark' => '', 'module' => '1', 'en_title' => NULL, 'content' => NULL, 'attrtext' => NULL, 'en_content' => NULL, 'price' => '0.01', 'originalCost' => '0', 'affmoney' => '0.00', 'discount' => '0.88', 'cost' => '0.00', 'img_url' => '', 'mimg_url' => '', 'is_hot' => '0', 'is_top' => '0', 'is_rec' => '0', 'status' => '-1', 'permission' => '-1', 'socre' => '0', 'buy_socre' => '0', 'sort' => '100', 'salecount' => '0', 'out_link' => '', 'table_name' => '2', 'extendsField' => '', 'category_id' => '0', 'brand_id' => '0', 'site_id' => '143', 'stocks' => '0', 'sales' => '0', 'attrList' => NULL, 'create_time' => NULL, 'update_time' => NULL, 'supplier_id' => '1', 'shipping_id' => '0', 'weight' => '0.00', 'relateids' => NULL, 'shipping_fee' => '0.00', 'integral_pay' => '0', 'need_shipping' => '1', 'display' => '1', 'limitBuy' => '0', 'affpass' => '0', 'page_title' => '', 'description' => '', 'area' => NULL, 'noshippingfee' => '0', 'code' => '', 'is_cashback' => '0', 'coupon_id' => '0', 'servicecard_id' => '0', 'roleid' => '0', 'levelBuy' => '0', 'minBuy' => '0', 'unit' => '', 'autoconfirm' => '0', 'oldid' => '0', 'starttime' => '0', 'timeout' => '0', 'systemagent' => '0', 'systemagentprice' => '[]', 'systemagentrenewprice' => '[]', 'levelpriceconfig' => NULL, 'numpriceconfig' => NULL, 'commision' => '0.00', 'baidumapX' => '113.751686', 'baidumapY' => '23.025398', 'shopname' => NULL, 'relate_id' => '0', 'Merchantrebates' => '0', 'Consumerebates' => '0', 'mycategory_id' => '0', 'RebateModel' => '1', 'geohash' => 'uxbrupcpyz', );

		//查找当前供货商的二维码

		$pdb = M('Product');

		$qr = $pdb -> where(array('status' => -1, 'supplier_id' => $supplier_id )) -> select();

		if(!$qr){

			$array['supplier_id'] = $supplier_id;

			$array['RebateModel'] = 1;

			$pdb -> add($array);

			

			$array['RebateModel'] = 2;

			$pdb -> add($array);

			$array['RebateModel'] = 0;

			$pdb -> add($array);

		}else{

			return false;

		}

		 

		

	}

}

